﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
namespace WPFExcelReport
{
    class DSTAB50AV95Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        string ABPAUFFKRS;
        string ABPAUFFLFD;
        string ABPAUFFPOS;
        string WBBTP;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        string Today
        {
            get
            {
                DateTime now = DateTime.Now;
                return now.Year.ToString() + Right("00" + now.Month.ToString(), 2) + Right("00" + now.Day.ToString(), 2);
            }
        }
        #endregion
        public DSTAB50AV95Report()
            : base()
        {
        }
        public DSTAB50AV95Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
        }
        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 4, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["DSTPLAN_AB50"];
                //exAgent.DataTableToExcel(dtSoruce, 2, 1);
                DataTable dtOutput = new DataTable(); 
                dtOutput.Columns.Add("ARTICLE", typeof(System.String));
                dtOutput.Columns.Add("YARN", typeof(System.String));
                dtOutput.Columns.Add("FDDIS", typeof(System.String));
                dtOutput.Columns.Add("QTY", typeof(System.String));
                dtOutput.Columns.Add("UNIT", typeof(System.String));
                dtOutput.Columns.Add("REQDATE", typeof(System.String));
                dtOutput.Columns.Add("JW", typeof(System.String));
                dtOutput.Columns.Add("ABPORDER", typeof(System.String)); 
                dtOutput.Columns.Add("CYARN", typeof(System.String));
                dtOutput.Columns.Add("ABPLIEFDAT", typeof(System.String)); 
                dtOutput.Columns.Add("ABPERLKZ", typeof(System.String));
                dtOutput.Columns.Add("ABPERLDAT", typeof(System.String));
                dtOutput.Columns.Add("NAME", typeof(System.String)); 
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);
                foreach (DataRow rw in dtOutput.Rows)
                { 
                    rw["YARN"] = GetGB2312Value(rw["YARN"].ToString()).Trim(); 
                    rw["NAME"] = GetGB2312Value(rw["NAME"].ToString()).Trim(); 
                }
                exAgent.DataTableToExcel(dtOutput, xStart, yStart);
                int count = dtOutput.Rows.Count;
                exAgent.BatchConvert2Number(4, 4, count + 4, 4);
                exAgent.SetCellValue(3, 1, "");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {

                strExcelFile = @"DST\DSTStandard.xls";
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                filename = @"染色查询_" + filedate;
                exAgent.CheckWorkBook(filename);
                LocalFile(filename);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
 

        private string CreateFile(string orderno)
        {
            string filename = "";
            string sheetName = "";
            string Standard = "AB50AV95DOC";
            string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\DST\DSTStandard.xls");
            try
            {
                string pwd = "";
                sheetName = orderno;
                exAgent.Open(filePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                string newfilename = orderno;
                exAgent.CreateNewSheet(newfilename, Standard, orderno.Replace(".xls", ""), pwd);
                filename = exAgent.FileName;
                FillInExcelData(null,dsSource,5,1);
                string localFile = exAgent.FileName;
                exAgent.Save();
                exAgent.Close();
                //System.Diagnostics.Process.Start("EXCEL.EXE", localFile);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }

        private void LocalFile(string ordernumer)
        { 
            string fileabsname = ordernumer + ".xls";
            string localFile = @"C:\Windows\Temp\" + fileabsname;
            string tempfile = "";
            try
            {
                tempfile = CreateFile(fileabsname);
                localFile = @"C:\IntexReport\Planning\" + fileabsname;
                if (System.IO.File.Exists(localFile))
                {
                    System.IO.File.Delete(localFile);
                }
                File.Move(tempfile, localFile);
                System.Diagnostics.Process.Start("EXCEL.EXE", localFile);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        } 
    }
}